package com.example.chamberlainserver.Service.impl;

import com.example.chamberlainserver.Entry.ShiftTime;
import com.example.chamberlainserver.Entry.User;
import com.example.chamberlainserver.Service.ShiftTimeService;
import com.example.chamberlainserver.Service.UserService;
import com.example.chamberlainserver.dto.PageResult;
import com.example.chamberlainserver.mapper.ShiftTimeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 班次时间服务实现类
 */
@Service
public class ShiftTimeServiceImpl implements ShiftTimeService {

    @Autowired
    private ShiftTimeMapper shiftTimeMapper;
    
    @Autowired
    private UserService userService;

    @Override
    public boolean addShiftTime(ShiftTime shiftTime) {
        shiftTime.setId(UUID.randomUUID().toString());
        shiftTime.setCreatedAt(LocalDateTime.now());
        shiftTime.setUpdatedAt(LocalDateTime.now());
        if (shiftTime.getIsActive() == null) {
            shiftTime.setIsActive(true);
        }
        return shiftTimeMapper.insertShiftTime(shiftTime) > 0;
    }

    @Override
    public ShiftTime getShiftTimeById(String id) {
        ShiftTime shiftTime = shiftTimeMapper.getShiftTimeById(id);
        if (shiftTime != null && shiftTime.getCreatedBy() != null) {
            User creator = userService.getUserById(shiftTime.getCreatedBy());
            shiftTime.setCreator(creator);
        }
        return shiftTime;
    }

    @Override
    public List<ShiftTime> getShiftTimesByType(String shiftType) {
        List<ShiftTime> shiftTimes = shiftTimeMapper.getShiftTimesByType(shiftType);
        
        for (ShiftTime shiftTime : shiftTimes) {
            if (shiftTime.getCreatedBy() != null) {
                User creator = userService.getUserById(shiftTime.getCreatedBy());
                shiftTime.setCreator(creator);
            }
        }
        
        return shiftTimes;
    }

    @Override
    public List<ShiftTime> getActiveShiftTimes() {
        List<ShiftTime> shiftTimes = shiftTimeMapper.getActiveShiftTimes();
        
        for (ShiftTime shiftTime : shiftTimes) {
            if (shiftTime.getCreatedBy() != null) {
                User creator = userService.getUserById(shiftTime.getCreatedBy());
                shiftTime.setCreator(creator);
            }
        }
        
        return shiftTimes;
    }

    @Override
    public List<ShiftTime> getAllShiftTimes() {
        List<ShiftTime> shiftTimes = shiftTimeMapper.getAllShiftTimes();
        
        for (ShiftTime shiftTime : shiftTimes) {
            if (shiftTime.getCreatedBy() != null) {
                User creator = userService.getUserById(shiftTime.getCreatedBy());
                shiftTime.setCreator(creator);
            }
        }
        
        return shiftTimes;
    }

    @Override
    public boolean updateShiftTime(ShiftTime shiftTime) {
        shiftTime.setUpdatedAt(LocalDateTime.now());
        return shiftTimeMapper.updateShiftTime(shiftTime) > 0;
    }

    @Override
    public boolean deleteShiftTime(String id) {
        return shiftTimeMapper.deleteShiftTimeById(id) > 0;
    }

    @Override
    public boolean updateShiftTimeStatus(String id, Boolean isActive) {
        return shiftTimeMapper.updateShiftTimeStatus(id, isActive) > 0;
    }

    @Override
    public PageResult<ShiftTime> getPagedShiftTimeList(Map<String, Object> params) {
        // 查询总记录数
        Long total = shiftTimeMapper.getPagedShiftTimeCount(params);
        
        // 查询数据列表
        List<ShiftTime> shiftTimes = shiftTimeMapper.getPagedShiftTimeList(params);
        
        // 为每个记录加载创建人信息
        for (ShiftTime shiftTime : shiftTimes) {
            if (shiftTime.getCreatedBy() != null) {
                User creator = userService.getUserById(shiftTime.getCreatedBy());
                shiftTime.setCreator(creator);
            }
        }
        
        Integer current = (Integer) params.getOrDefault("current", 1);
        Integer pageSize = (Integer) params.getOrDefault("pageSize", 10);
        
        return new PageResult<>(current, pageSize, total, shiftTimes);
    }

    @Override
    public boolean validateShiftTimeConfig(ShiftTime shiftTime) {
        if (shiftTime == null) {
            return false;
        }
        
        // 验证班次名称
        if (shiftTime.getShiftName() == null || shiftTime.getShiftName().trim().isEmpty()) {
            return false;
        }
        
        // 验证开始时间和结束时间
        if (shiftTime.getStartTime() == null || shiftTime.getEndTime() == null) {
            return false;
        }
        
        // 验证时间逻辑（结束时间应该晚于开始时间，或者是跨天班次）
        LocalTime startTime = shiftTime.getStartTime();
        LocalTime endTime = shiftTime.getEndTime();
        
        // 如果结束时间早于开始时间，认为是跨天班次，这是允许的
        // 例如：夜班 22:00-06:00
        
        return true;
    }
}